﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// AGV信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AGVCorrelationController : ControllerBase
    {
        private readonly IAGVCorrelationRepository iAGVCorrelationRepository;
        private readonly ILogRepository logRepository;
        public AGVCorrelationController(IAGVCorrelationRepository iAGVCorrelationRepository, ILogRepository logRepository)
        {
            this.iAGVCorrelationRepository = iAGVCorrelationRepository;
            this.logRepository = logRepository;
        }

        /// <summary>
        /// 添加AGV信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddAGVPointMessage(TAGVPointMessage message)
        {

            ApiResult apiresult = new ApiResult();
            bool IsOK = iAGVCorrelationRepository.AddAGVPointMessage(message);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了AGV点位信息" + message.PointName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "添加成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "添加失败！" + message;
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 修改AGV信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdAGVPointMessage(TAGVPointMessage message)
        {

            ApiResult apiresult = new ApiResult();
            bool IsOK = iAGVCorrelationRepository.UpdAGVPointMessage(message);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了AGV点位信息" + message.PointName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "修改成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "修改失败！" + message;
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 删除AGV信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAGVPointMessage(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiresult = new ApiResult();
            bool IsOK = iAGVCorrelationRepository.DelAGVPointMessage(id);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了AGV信息";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "删除成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "删除失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 获取所有AGV信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllMessage(int pageIndex, int pageSize)
        {
            var res = iAGVCorrelationRepository.GetAllMessage(pageIndex,pageSize,out int totalCount);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.count = totalCount;
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.count = 0;
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMessageById(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = iAGVCorrelationRepository.GetMessageById(id);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }


        /// <summary>
        /// 添加AGV任务类型
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddAGVBinDingPoint(TAGVBinDingPoint model)
        {

            ApiResult apiresult = new ApiResult();
            bool IsOK = iAGVCorrelationRepository.AddAGVBinDingPoint(out string msg,model);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "绑定了任务类型" + model.TaskTypeName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "添加成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = msg;
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 修改AGV任务类型
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdAGVBinDingPoint(TAGVBinDingPoint model)
        {

            ApiResult apiresult = new ApiResult();
            bool IsOK = iAGVCorrelationRepository.UpdAGVBinDingPoint(model);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了任务类型" + model.TaskTypeName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "修改成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "修改失败！";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 删除AGV任务类型
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAGVBinDingPoint(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiresult = new ApiResult();
            bool IsOK = iAGVCorrelationRepository.DelAGVBinDingPoint(id);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了AGV绑定信息";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "删除成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "删除失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 查询全部AGV任务类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllDingPoints(int pageIndex, int pageSize)
        {
            var res = iAGVCorrelationRepository.GetAllDingPoints(pageIndex,pageSize,out int totalCount);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.count = totalCount;
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.count = 0;
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDingPointById(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = iAGVCorrelationRepository.GetDingPointById(id);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 获取AGV分组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAGVGroup()
        {

            var res = iAGVCorrelationRepository.GetAGVGroup();
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
    }
}
